package com.xlx.openLink.controller;

import com.xlx.openLink.base.BaseDateMessage;
import com.xlx.openLink.base.BaseMessage;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.entity.pojo.Alarm;
import com.xlx.openLink.entity.pojo.Manager;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.service.AlarmService;
import com.xlx.openLink.vo.RequestAlarmList;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@Slf4j
@RequestMapping("/alarm")
public class AlarmController {


    @Autowired
    private AlarmService alarmService;

    /**
     * 分页操作
     *
     * @param requestAlarmList
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParam(name = "requestAlarmList", value = "分页请求参数", required = true, dataType = "RequestAlarmList")
    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestAlarmList requestAlarmList) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(alarmService.listByPage(requestAlarmList));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);

        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseDateMessage;
    }

    /**
     * 告警处理
     *
     * @param alarm
     * @return
     */
    @ApiOperation(value = "处理告警", notes = "处理告警")
    @ApiImplicitParam(name = "alarm", value = "告警实体", required = true, dataType = "Alarm")
    @PostMapping("/optionAlarm")
    @ResponseBody
    public BaseMessage optionAlarm(@RequestBody Alarm alarm) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            baseMessage = alarmService.optionAlarmInfo(alarm);
        } catch (ThreadRunTimeExcption excption) {
            baseMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseMessage;
    }


    /**
     * 删除告警
     *
     * @param pkGuid
     * @return
     */
    @ApiOperation(value = "删除告警", notes = "删除告警")
    @ApiImplicitParam(name = "pkGuid", value = "告警ID", required = true, dataType = "String")
    @GetMapping("/delAlarm")
    @ResponseBody
    public BaseMessage delAlarm(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            alarmService.delAlarm(pkGuid);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
        } catch (ThreadRunTimeExcption excption) {
            baseMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseMessage;
    }

    //删除全部
    @ResponseBody
    @GetMapping("/delall")
    public BaseMessage delall() {
        BaseMessage baseMessage = new BaseMessage();
        try {
            alarmService.delAll();
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
        } catch (ThreadRunTimeExcption excption) {
            baseMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseMessage;
    }

    //删除全部
    @ResponseBody
    @PostMapping("/dealAll")
    public BaseMessage dealAll(@RequestBody Manager manager) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            alarmService.dealAll(manager);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
        } catch (ThreadRunTimeExcption excption) {
            baseMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseMessage;
    }

}
